<?php 
namespace app\index\model;
use think\Model;
use think\Db;
class Page extends Model
{
	public $table; 		//表名
	public $page; 		//页码
	public $size; 		//每页显示条数
	public $count; 		//总数
	public $page_total;  //总页码
	public $where;  		//条件
	public function index($arr=array())
	{
		// print_r($arr['table']);die;
		if(empty($arr)){
			return '参数错误';
		}
		if(empty($arr['table'])){
			return '参数错误';
		}
		$this->table = $arr['table'];
		// echo $arr['table'];die;
		$this->page = !empty($arr['page'])&&isset($arr['page'])?$arr['page']:1;

		$this->size = !empty($arr['size'])&&isset($arr['size'])?$arr['size']:5;

		$this->where = !empty($arr['where'])&&isset($arr['where'])?$arr['where']:'1=1';

		$this->value = !empty($arr['value'])&&isset($arr['value'])?$arr['value']:'*';
		// echo "string";die;
		$this->pcount();
		$sql = 'select * from '.$this->table.' where 1=1 '.$this->where.' limit '.($this->page-1)*$this->size.','.$this->size;
		$page = Db::query($sql);
		
		foreach($page as $k=>$v){
			if($v['startTime']>time()){
    			$page[$k]['bi']='a';//即将上线
    		}else if($v['surplusMoney']==0){
    			// print_r($v);die;
    			$time = $v['updateTime']+($v['deadLine']+1)*30;
    			if($time>time()){
    				$page[$k]['bi'] = 'b';//回款中
    			}else{
    				$page[$k]['bi'] = 'c';//已结束
    			}
    		}else if($v['endTime']<time()){
    			$time = $v['updateTime']+($v['deadLine']+1)*30;
    			if($time>time()){
    				$page[$k]['bi'] = 'b';//回款中
    			}else{
    				$page[$k]['bi'] = 'c';//已结束
    			}
    		}else if($v['surplusMoney']>0){
    			$page[$k]['bi']=floor((1-$v['surplusMoney']/$v['totalSum'])*100);//百分比

    		}
    		// var_dump($page);
		}
		// var_dump($page);die;
		$datas['data']=$page;
		$datas['page']=$this->page;
		$datas['page_total']=$this->page_total;
		return $datas;
	}

	public function pcount()
	{
		$sql = 'select * from '.$this->table.' where 1=1 '.$this->where;
		// echo $sql;die;
		$data = Db::query($sql);
		$this->count = count($data);
		$this->page_total = ceil($this->count/$this->size);
	}
}

 ?>